#include "Connection.h"
#include "DebugTool.h"
#include <QMutex>
#include <QFileInfo>

Page page = NonePage;

MQSortFilterProxyModel *proxyModel=0;
QSqlQueryModel *bookModel=0;
QSqlQueryModel *loanModel=0;
QSqlQueryModel *userModel=0;
QStandardItemModel *temp=0;
QStringListModel *cmbModel = 0;

Connection *Connection::self = 0;

Connection::Connection() {
}

Connection *Connection::Instance() {
  static QMutex mutex;
  if (!self) {
    QMutexLocker locker(&mutex);
    if (!self) {
      self = new Connection;
    }
  }
  return self;
}

void Connection::setDataBase() {
  db = QSqlDatabase::addDatabase("QSQLITE");

  db.setDatabaseName("../database/mybms.db");

  bool ok = db.open();
  if (ok == false) {
    cout << "数据库打开失败！";
    return;
  }

//  cout << db.tables();
}

void setBookHeaderData(QAbstractItemModel *model) {
  Qt::Orientation hor = Qt::Horizontal;
  model->setHeaderData(BkId, hor, "ISBN");
  model->setHeaderData(BkName, hor, "书名");
  model->setHeaderData(BkAuthor, hor, "作者");
  model->setHeaderData(BkPub, hor, "出版社");
  model->setHeaderData(BkPubDate, hor, "出版日期");
  model->setHeaderData(BkAvail, hor, "是否可借");
  model->setHeaderData(BkStore, hor, "库存");
  model->setHeaderData(BkTotal, hor, "总数");
  model->setHeaderData(BkPrice, hor, "价格");
}

void setLoanHeaderData(QAbstractItemModel *model)
{
  Qt::Orientation hor = Qt::Horizontal;
  model->setHeaderData(LnUser, hor, "用户");
  model->setHeaderData(LnBookID, hor, "ISBN");
  model->setHeaderData(LnBookName, hor, "书名");
  model->setHeaderData(LnLDate, hor, "借阅日期");
  model->setHeaderData(LnRDate, hor, "应还日期");
}
